package com.hxzy.yygh.hosp.controller.api;

import com.hxzy.yygh.common.exception.YyghException;
import com.hxzy.yygh.common.helper.HttpRequestHelper;
import com.hxzy.yygh.common.result.Result;
import com.hxzy.yygh.common.result.ResultCodeEnum;
import com.hxzy.yygh.common.util.MD5;
import com.hxzy.yygh.hosp.service.DepartmentService;
import com.hxzy.yygh.hosp.service.HospitalService;
import com.hxzy.yygh.hosp.service.HospitalSetService;
import com.hxzy.yygh.hosp.service.ScheduleService;
import com.hxzy.yygh.model.hosp.Department;
import com.hxzy.yygh.model.hosp.Hospital;
import com.hxzy.yygh.model.hosp.Schedule;
import com.hxzy.yygh.vo.hosp.DepartmentQueryVo;
import com.hxzy.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        //1.签名验证
        Map<String, Object> paramMap = this.verifySignature(request);

        //2.传输过程中,url编码将”+“ 解析为 ” “了，因此将其转回来
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        paramMap.put("logoData",logoData);

        //3.调用service的保存方法
        hospitalService.save(paramMap);

        //4.返回成功状态码
        return Result.ok();
    }

    //查询医院信息接口
    @PostMapping("hospital/show")
    public Result showHosp(HttpServletRequest request){
        //1.签名验证
        Map<String, Object> paramMap = this.verifySignature(request);

        //2.获取医院编号
        String hoscode = (String) paramMap.get("hoscode");

        //3.调用service的根据医院编号查询的方法
        Hospital hospital = hospitalService.getByHoscode(hoscode);

        //4.返回成功状态码
        return Result.ok(hospital);
    }

    //上传科室接口
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //1.签名验证
        Map<String, Object> paramMap = this.verifySignature(request);

        //2.调用service方法实现根据医院编号查询
        departmentService.saveDepartment(paramMap);

        //3.返回成功状态码
        return Result.ok();
    }

    //查询医院科室接口
    @PostMapping("department/list")
    public Result showDepartment(HttpServletRequest request){
        //1.验证签名
        Map<String, Object> paramMap = this.verifySignature(request);

        //2.获取医院编号、当前页、每页记录数
        String hoscode = (String) paramMap.get("hoscode");
        int page = Integer.parseInt((String) paramMap.get("page"));
        int limit = Integer.parseInt((String) paramMap.get("limit"));

        //3.封装科室vo查询对象
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        //3.调用service查询方法
        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);

        //4.返回分页对象
        return Result.ok(pageModel);
    }

    //删除医院科室信息接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //1.获取签名
        Map<String, Object> paramMap = this.verifySignature(request);

        //2.获取医院编号、科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");

        //3.调用service删除科室方法
        departmentService.removeDepartment(hoscode, depcode);

        //4.返回成功状态码
        return Result.ok();

    }

    //上传排班信息
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //1.验证签名
        Map<String, Object> paramMap = this.verifySignature(request);

        //2.调用service的上传排班方法
        scheduleService.saveSchedule(paramMap);

        //3.返回成功状态码
        return Result.ok();
    }

    //分页展示排班信息接口
    @PostMapping("schedule/list")
    public Result showSchedule(HttpServletRequest request){
        //1.签名验证
        Map<String, Object> paramMap = this.verifySignature(request);

        //2.获取医院编号、当前页、每页记录数
        String hoscode = (String) paramMap.get("hoscode");
        //String depcode = (String) paramMap.get("depcode");
        int page = Integer.parseInt((String) paramMap.get("page"));
        int limit = Integer.parseInt((String) paramMap.get("limit"));

        //3.封装科室vo查询对象
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);

        //3.调用service查询方法
        Page<Schedule> pageModel = scheduleService.findPageDepartment(page, limit, scheduleQueryVo);

        //4.返回分页对象
        return Result.ok(pageModel);

    }

    //删除排班信息接口
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        //1.签名验证
        Map<String, Object> paramMap = this.verifySignature(request);

        //2.获取医院编号、排班编号
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");

        //3.调用service删除排班信息接口
        scheduleService.removeSchedule(hoscode,hosScheduleId);

        //4.返回成功状态码
        return Result.ok();
    }

    //验证签名方法
    public Map<String, Object> verifySignature(HttpServletRequest request){
        //1.获取传递过来的医院信息
        Map<String, String[]> requestParameterMap = request.getParameterMap();

        //2.转换map集合的存储格式(String:String[] ==> String:Object)
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestParameterMap);

        //3.获取医院系统传过来的医院编号和签名
        String hoscode = (String) paramMap.get("hoscode");
        String hospSign = (String) paramMap.get("sign");//传递过来的签名是已经被MD5加密过的

        //4.根据医院编号查询其医院设置数据库中的签名
        String signKey = hospitalSetService.getSignKey(hoscode);

        //5.将从数据库中查询出来的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //6.判断与传递过来的签名是否一致
        if(!hospSign.equals(signKeyMd5)){
            //如果签名不一致，则抛出异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //7.返回数据
        return paramMap;
    }
}
